//
//  HWAssetsTopHeader.swift
//  HWPromoteProjectAPP
//
//  Created by 王昌健 on 2022/1/27.
//  Copyright © 2022 王昌健. All rights reserved.
//

import UIKit

class HWAssetsTopHeader: UITableViewHeaderFooterView {
    /// 推广金额
    var promoteTitleLabel:UILabel!
    /// 金额
    var priceLabel:UILabel!
    /// 申请结算
    var applyButton:UIButton!
    /// 分享佣金
    var shareTitleLabel:UILabel!
    /// 分享佣金
    var sharePriceLabel:UILabel!
    var lineView:UIView!
    /// 提现佣金
    var withdrawTitleLabel:UILabel!
    /// 提现佣金
    var withdrawPriceLabel:UILabel!
    var assetsModel:HWAssetsModel? {
        didSet {
            priceLabel.text = assetsModel?.money
            sharePriceLabel.text = assetsModel?.shareMoney
            withdrawPriceLabel.text = assetsModel?.withdraw
        }
    }
    
    var applyButtonClick:(()->())?
    
    class func headerOrFooterWithTableView(tableView:UITableView) -> (HWAssetsTopHeader) {
        let reUse:String = "HWAssetsTopHeader"
        
        var headerOrFooter = tableView.dequeueReusableHeaderFooterView(withIdentifier: reUse)
        
        if headerOrFooter == nil {
            headerOrFooter = HWAssetsTopHeader.init(reuseIdentifier: reUse)
        }
        
        return headerOrFooter as! HWAssetsTopHeader
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        initUI()
        initFrame()
    }
    
    
    func initUI() -> Void {
        
        contentView.backgroundColor = .colorWithHexString(hexString: "#5879FF")
        
        promoteTitleLabel = HWLabel.createBoldLabel(title: "推广金额 (元)", color: "#FFFFFF", font: 12*widthScale)
        contentView.addSubview(promoteTitleLabel)
        
        priceLabel = HWLabel.createBoldLabel(title: "2400.00", color: "#FFFFFF", font: 36*widthScale)
        contentView.addSubview(priceLabel)
        
        applyButton = UIButton(type: .custom)
        applyButton.setTitle("申请结算", for: .normal)
        applyButton.titleLabel?.font = .boldSystemFont(ofSize: 14*widthScale)
        applyButton.setTitleColor(.colorWithHexString(hexString: "#5559EA"), for: .normal)
        applyButton.layer.cornerRadius = 16*widthScale
        applyButton.backgroundColor = .white
        applyButton.addTarget(self, action: #selector(applyButtonClick(_:)), for: .touchUpInside)
        contentView.addSubview(applyButton)
        
        shareTitleLabel = HWLabel.createBoldLabel(title: "分享佣金", color: "#FFFFFF", font: 12*widthScale)
        contentView.addSubview(shareTitleLabel)
        
        sharePriceLabel = HWLabel.createBoldLabel(title: "66.00", color: "#FFFFFF", font: 22*widthScale)
        contentView.addSubview(sharePriceLabel)
        
        lineView = UIView()
        lineView.backgroundColor = .white
        contentView.addSubview(lineView)
        
        withdrawTitleLabel = HWLabel.createBoldLabel(title: "已提现佣金", color: "#FFFFFF", font: 12*widthScale)
        contentView.addSubview(withdrawTitleLabel)
        
        withdrawPriceLabel = HWLabel.createBoldLabel(title: "20.00", color: "#FFFFFF", font: 22*widthScale)
        contentView.addSubview(withdrawPriceLabel)
    }
    
    func initFrame() -> Void {
        
        promoteTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(20*widthScale)
            make.top.equalTo(74*widthScale)
        }
        
        priceLabel.snp.makeConstraints { make in
            make.left.equalTo(promoteTitleLabel)
            make.top.equalTo(promoteTitleLabel.snp.bottom).offset(11*widthScale)
        }
        
        applyButton.snp.makeConstraints { make in
            make.centerY.equalTo(priceLabel)
            make.width.equalTo(78*widthScale)
            make.height.equalTo(32*widthScale)
            make.right.equalTo(-20*widthScale)
        }
        
        shareTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(69*widthScale)
            make.top.equalTo(priceLabel.snp.bottom).offset(39*widthScale)
        }
        
        sharePriceLabel.snp.makeConstraints { make in
            make.centerX.equalTo(shareTitleLabel)
            make.top.equalTo(shareTitleLabel.snp.bottom).offset(4*widthScale)
        }
        
        lineView.snp.makeConstraints { make in
            make.width.equalTo(1*widthScale)
            make.height.equalTo(30*widthScale)
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-27*widthScale)
        }
        
        withdrawTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(lineView.snp.right).offset(63*widthScale)
            make.centerY.equalTo(shareTitleLabel)
        }
        
        withdrawPriceLabel.snp.makeConstraints { make in
            make.centerX.equalTo(withdrawTitleLabel)
            make.top.equalTo(withdrawTitleLabel.snp.bottom).offset(4*widthScale)
        }
    }

}

extension HWAssetsTopHeader {
    @objc func applyButtonClick(_:UIButton) -> Void {
        self.applyButtonClick?()
    }
}
